package com.wan37.client.message.received;

import com.google.protobuf.Message;
import com.wan37.client.handler.ClientHandler;
import com.wan37.client.global.reference.SpringContextHolder;
import com.wan37.client.service.FightingService;
import com.wan37.client.service.cache.ChatCache;
import com.wan37.client.service.cache.RoleServiceCache;
import com.wan37.client.service.cache.UserServiceCache;
import com.wan37.protobuf.constant.Common;
import com.wan37.protobuf.constant.CommonFighting;
import com.wan37.protobuf.constant.PtoNum;
import com.wan37.protobuf.message.client2server.game.Game;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author : luoyong
 * @date : 2020-07-01 16:05
 **/
@Component(PtoNum.GAMESRESPONSE + "")
public class GameSResponse implements ReceiveMsg {

    private final static Logger logger = LoggerFactory.getLogger(GameSResponse.class);
    private ClientHandler clientHandler = ClientHandler.getInstance();
    @Autowired
    private ChatCache chatCache;
    @Autowired
    private UserServiceCache userServiceCache;
    @Autowired
    private RoleServiceCache roleServiceCache;

    @Override
    public void execute(Message msg) {
        /**
         * 逻辑服务器处理后，返回的消息
         */
        Game.SResponse sResponse = (Game.SResponse) msg;
        int code = sResponse.getCode();
        String content = sResponse.getContent();
        switch (code) {
            //用户请求切换场景成功
            case Common.GAME_MOVE_SUCCESS:
                logger.info("场景切换成功，当前场景：" + content);
                clientHandler.current_scene = content;
                break;

            //用户切换场景失败
            case Common.GAME_MOVE_ERROR:
                logger.info("场景切换失败，当前场景：" + content);
                break;

            case Common.GAME_CHAT_SUCCESS:
                logger.info("请求NPC聊天成功！！");
                logger.info("NPC说: " + content);
                break;

            case Common.GAME_USER_LOGIN_INIT_SUCCESS:
                logger.info("用户初始化成功！！");
                clientHandler.isRole = true;
                clientHandler.sendAoiMessage();
                ChatCache.ChatIn chatIn = chatCache.new ChatIn();
                chatCache.getUserIdToChatIn().put(clientHandler.selfId, chatIn);
                break;
            case Common.GAME_USER_LOGIN_INIT_ERROR:
                logger.info("{}", sResponse.getContent());
                break;
            case Common.GAME_BOSS_SCENE_SUCCESS:
                logger.info("临时boss场景创建成功，场景id：" + content);
                clientHandler.current_scene = content;
                /**
                 * 表明进入boss场景，获取技能完成，需要触发自动战斗
                 */
                clientHandler.fightType = CommonFighting.PVE;
                FightingService fightingService = SpringContextHolder.getBean("pVEFightingService");
                fightingService.fighting();
                break;
            case Common.GAME_PVP_SCENE_SUCCESS:
                logger.info("临时PVP战斗场景创建成功，场景id：" + content);
                clientHandler.current_scene = content;
                /**
                 * 表明进入pvp战斗场景，获取技能完成，需要触发自动战斗
                 */
                clientHandler.fightType = CommonFighting.PVP;
                FightingService fightingService1 = SpringContextHolder.getBean("pVPFightingService");
                fightingService1.fighting();
                break;
            case Common.GAME_CREATE_ROLE:
                logger.info("创建角色成功！！");
                clientHandler.sendUserLoginInitMsg(clientHandler.selfId);
                break;
            case Common.OTHER_USER_MOVE:
                for (String roleId : userServiceCache.getUserInMap().get(sResponse.getContent()).getRoleIdList()){
                    roleServiceCache.getRoleInMap().remove(roleId);
                }
                userServiceCache.getUserInMap().remove(sResponse.getContent());
                logger.info("{} 用户已经离开该场景", sResponse.getContent());
                break;
            case Common.USER_KILL_BOSS:
                logger.info(sResponse.getContent());
                break;
            default:
                logger.info("错误的 Code, code : {}", code);
                break;
        }
    }
}
